import 'dart:io';

import 'package:dio/dio.dart';
import 'package:fsharing/core/services/auth.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';

class UserRealnameModel extends ViewModel {
  final Auth auth;

  String cardNo;
  String cardType = "ID_CARD_2";
  String name;
  String checkCode;
  File _frontFile;
  File _backFile;

  String _errorMessage;

  UserRealnameModel(this.auth) : super();

  File get frontFile => _frontFile;

  set frontFile(File value) {
    _frontFile = value;
    notifyListeners();
  }

  File get backFile => _backFile;

  set backFile(File value) {
    _backFile = value;
    notifyListeners();
  }

  String get errorMessage => _errorMessage;

  Future<ViewState> userRealname() async {
    viewState = ViewState.busy;
    try {
      if (checkCode == null || checkCode.isEmpty) {
        throw '请输入验证码';
      }
      var params = <String, dynamic>{};
      params['cardNo'] = cardNo;
      params['cardType'] = cardType;
      params['name'] = name;
      params['checkCode'] = checkCode;
      params['frontFile'] = _frontFile;
      params['backFile'] = _backFile;
      await auth.userRealname(params);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
